package com.aliyun.messagemgt.application.service.factory;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.messagemgt.application.service.MsgPushChannel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IDEA
 *
 * @author:zhangxingxing 0027012120
 * @date:2019/1/24
 * @time:17:05
 */
@Component
public class PushChannelFactory implements InitializingBean {

    private static final Logger logger = LoggerFactory.getLogger(PushChannelFactory.class);
    @Autowired
    private List<MsgPushChannel> channelList;

    private Map<Integer, MsgPushChannel> channelMap = new HashMap<>();

    public MsgPushChannel getPushChannel(Integer channelType) {
        MsgPushChannel channel = channelMap.get(channelType);
        if (channel == null) {
            logger.error("{} 没有对应的通道。", channelType);
        }
        return channel;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        channelList.forEach(channel -> {
            channelMap.put(channel.getChannelType(), channel);
        });
        logger.info("channelMap {}", JSONObject.toJSONString(channelMap));
    }
}
